﻿using System.Text;
using System.Xml;

namespace Eneta.Portal.Forum.Utility
{
    public class SharePointMenu
    {
        // Fields
        private XmlElement _currentNode;
        private string _header;
        private XmlElement _rootNode;
        private XmlDocument _xmlDocument;

        // Methods
        public SharePointMenu(string title)
        {
            CreateHeader(title);
            CreateXmlDocument();
        }

        public void AddLabel(string title)
        {
            var element = _xmlDocument.CreateElement("ie:menuitem", "http://www.tempuri.com");
            element.SetAttribute("type", "label");
            element.InnerText = title;
            _currentNode.AppendChild(element);
        }

        public void AddMenuItem(string title, string url)
        {
            AddMenuItem(title, url, string.Empty);
        }

        public void AddMenuItem(string title, string url, string imageUrl)
        {
            var element = _xmlDocument.CreateElement("ie:menuitem", "http://www.tempuri.com");
            element.SetAttribute("id", "MSO_MyMenu_Link1");
            element.SetAttribute("title", title);
            if (imageUrl != string.Empty)
            {
                element.SetAttribute("iconSrc", imageUrl);
            }
            element.SetAttribute("onMenuClick", string.Format("javascript:window.location.href='{0}';", url));
            element.InnerText = title;
            _currentNode.AppendChild(element);
        }

        public void AddSeparator()
        {
            var element = _xmlDocument.CreateElement("ie:menuitem", "http://www.tempuri.com");
            element.SetAttribute("type", "separator");
            _currentNode.AppendChild(element);
        }

        public void AddSubMenu(string title)
        {
            AddSubMenu(title, string.Empty);
        }

        public void AddSubMenu(string title, string imageUrl)
        {
            XmlElement element = _xmlDocument.CreateElement("ie:menuitem", "http://www.tempuri.com");
            element.SetAttribute("type", "submenu");
            if (imageUrl != string.Empty)
            {
                element.SetAttribute("iconSrc", imageUrl);
            }
            _currentNode.AppendChild(element);
            _currentNode = element;
            AddLabel(title);
        }

        public void CloseSubMenu()
        {
            _currentNode = _rootNode;
        }

        private void CreateHeader(string title)
        {
            Header = string.Format("<div class=\"ms-HoverCellInActive\" onmouseover=\"this.className='ms-HoverCellActive'\"onmouseout=\"this.className='ms-HoverCellInActive'\"><a id=\"MSO_MyMenuLink\" title=\"{0}\" style=\"CURSOR: hand\"onclick=\"MSOWebPartPage_OpenMenu(MSO_MyMenu, this);\"tabindex=\"0\">{0}<img alt=\"{0}\" src=\"/_layouts/images/menudark.gif\"align=\"absBottom\"></a></div>", title);
        }

        private void CreateXmlDocument()
        {
            _xmlDocument = new XmlDocument();
            _rootNode = _xmlDocument.CreateElement("menu", "http://www.tempuri.com");
            _rootNode.SetAttribute("id", "MSO_MyMenu");
            _rootNode.SetAttribute("class", "ms-SrvMenuUI");
            _xmlDocument.AppendChild(_rootNode);
            _currentNode = _rootNode;
        }

        public override string ToString()
        {
            var builder = new StringBuilder();
            builder.Append(Header);
            builder.Append(_xmlDocument.InnerXml);
            return builder.ToString();
        }

        // Properties
        private string Header
        {
            get
            {
                return _header;
            }
            set
            {
                _header = value;
            }
        }
    }
}
